package com.skybai.common;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;


/**
 * @author sky-bai
 */
public class PdfReport {
    private static final Logger log = LoggerFactory.getLogger(PdfReport.class);
    private  Document document = new Document(PageSize.A4);
    // 定义全局的字体静态变量
    public static Font titleFont;
    public static Font headFont;
    public static Font keyFont;
    public static Font textFont;
    private PdfPTable table = new PdfPTable(2);
    private int count = 0;

    // 最大宽度
    private static int maxWidth = 520;
    // 静态代码块
    static {
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titleFont = new Font(bfChinese, 22, Font.BOLD);
            headFont = new Font(bfChinese, 20, Font.BOLD);
            keyFont = new Font(bfChinese, 14, Font.BOLD);
            textFont = new Font(bfChinese, 14,Font.NORMAL);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PdfReport() {
        try {
            table.setLockedWidth(true);
            table.setTotalWidth(600);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(ConfigUtil.file));
        } catch (DocumentException | FileNotFoundException e) {
            log.error("pdfWriter创建错误");
            e.printStackTrace();
        }
        document.open();
    }



    // 生成PDF文件
    public  void setTitle(String str){

        // 段落
        Paragraph paragraph = new Paragraph(str, titleFont);
        //设置文字居中 0靠左   1，居中     2，靠右
        paragraph.setAlignment(1);
        //设置左缩进
        paragraph.setIndentationLeft(12);
        //设置右缩进
        paragraph.setIndentationRight(12);
        //设置首行缩进
        paragraph.setFirstLineIndent(24);
        //行间距
        paragraph.setLeading(20f);
        //设置段落上空白
        //paragraph.setSpacingBefore(5f);
        //设置段落下空白
        paragraph.setSpacingAfter(10f);

        // 直线
        Paragraph p1 = new Paragraph();
        p1.add(new Chunk(new LineSeparator()));


        try {
            document.add(p1);
            document.add(paragraph);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public void setHead(String str){
        Paragraph paragraph = new Paragraph(str, headFont);
        //设置文字居中 0靠左   1，居中     2，靠右
        paragraph.setAlignment(1);
        //设置段落下空白
        paragraph.setSpacingAfter(20f);
        try {
            document.add(paragraph);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public  void setContent(String str){
        Paragraph paragraph = new Paragraph(str, textFont);
        paragraph.setKeepTogether(false);
        //设置文字居中 0靠左   1，居中     2，靠右
        paragraph.setAlignment(1);
        try {
            document.add(paragraph);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public void setDiv(Paragraph p) {
        PdfPCell cell = new PdfPCell();

        cell.setPhrase(p);
        cell.setBorder(0);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setMinimumHeight(250f);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setExtraParagraphSpace(10f);
        table.addCell(cell);
        count++;
    }



    public void close(){
        try {
            if(count % 2 != 0){
                PdfPCell cell = new PdfPCell();
                cell.setBorder(0);
                table.addCell(cell);
            }
            if(count == 0){
                document.add(new Paragraph("没有查询到任何内容",PdfReport.keyFont));
            }else{
                document.add(table);
            }
        } catch (DocumentException e) {
            log.error("table添加错误");
            e.printStackTrace();
        }finally {
            document.close();
        }
    }

}